//
//  CarViolationDetailViewController.m
//  EasyToo
//
//  Created by linshengqin on 13-9-3.
//
//

#import "CarViolationDetailViewController.h"
#import "CarViolationDetail2ViewController.h"
#import "DeviceUtil.h"
#import "CarViolationInfoViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CarViolationManager.h"
#define HEIGTH 40
@interface CarViolationDetailViewController ()

@end

@implementation CarViolationDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self initData];
    [self initView];
    

}
-(void)initView
{
    [self initMainView];
    [self initTableView:carViolation_model.violationInfoArray];
    [self initLabel:[carViolation_model.violationInfoArray count]+1]; //加一行描述
    [self initButton:[carViolation_model.violationInfoArray count]+1];
}
-(void) initData
{
    carViolation_model = [CarViolationModel sharedata];
    NSMutableString* detail = [[NSMutableString alloc] init];
    for( int i =0;i<[carViolation_model.violationInfoArray count];i++)
    {
        if( i == 0)
        {
            [detail appendString:[carViolation_model.violationInfoArray[i] objectAtIndex:0]];
            [detail appendString:@"|"];
            [detail appendString:[carViolation_model.violationInfoArray[i] objectAtIndex:6]];
            [detail appendString:@"|"];
            [detail appendString:[carViolation_model.violationInfoArray[i] objectAtIndex:7]];
            [detail appendString:@"|"];
            [detail appendString:[carViolation_model.violationInfoArray[i] objectAtIndex:8]];
            [detail appendString:@"|"];
        }
        else
        {
            [detail appendString:@"%"];
            [detail appendString:[carViolation_model.violationInfoArray[i] objectAtIndex:0]];
            [detail appendString:@"|"];
            [detail appendString:[carViolation_model.violationInfoArray[i] objectAtIndex:6]];
            [detail appendString:@"|"];
            [detail appendString:[carViolation_model.violationInfoArray[i] objectAtIndex:7]];
            [detail appendString:@"|"];
            [detail appendString:[carViolation_model.violationInfoArray[i] objectAtIndex:8]];
            [detail appendString:@"|"];
        }
    }
    carViolation_model.orderDetail = [[detail copy]autorelease];
    
    int total = 0;
    int principal = 0;
    int weiyue = 0;
    int service = 0;
    int postFee = [carViolation_model.postFee intValue];
    
    for( int i =0;i<[carViolation_model.violationInfoArray count];i++)
    {
        principal += [[carViolation_model.violationInfoArray[i] objectAtIndex:6] intValue];
        weiyue +=  [[carViolation_model.violationInfoArray[i] objectAtIndex:7] intValue];
        service +=  [[carViolation_model.violationInfoArray[i] objectAtIndex:8] intValue];
    }
    printf("%d,%d,%d,%d",principal,weiyue,service,postFee);
    total =(principal+weiyue+service+postFee)/100;
    carViolation_model.totalAmount = [NSString stringWithFormat:@"%d",total];
}

-(void)initMainView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self.view addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) initButton:(int)lines
{
    //下一步按钮
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [nextButton setFrame:CGRectMake(35, 190+40*lines, 250, 40)];
    
    [nextButton setTitle:@"下一步" forState:UIControlStateNormal];        //设置button在没有选中的时候显示的字体
    
    nextButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];         //设置button显示字体的大小
    
    [nextButton setBackgroundImage:[UIImage imageNamed:@"button_3"] forState:UIControlStateNormal];    //设置button背景显示图片
    
    [_bgScrollView addSubview:nextButton];
    
    [nextButton addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void) initLabel:(int)lines
{
    UILabel *label_user = [[[UILabel alloc]initWithFrame:CGRectMake(30, 10, 180, 30)]autorelease];
    [label_user setText:[NSString stringWithFormat:@"车牌号 :%@ 共有%d笔违章",carViolation_model.vehicleNo,[carViolation_model.violationInfoArray count]]];
    label_user.font = [UIFont fontWithName:@"Arial" size:12];   
    [_bgScrollView addSubview:label_user];
    
    UILabel *label_tip = [[[UILabel alloc]initWithFrame:CGRectMake(0, 40*lines+60, 180, 20)]autorelease];
    label_tip.font = [UIFont fontWithName:@"Arial" size:12];   //设置内容字体和字体大小
    label_tip.text = @"点击每项进入违章详单";
    [_bgScrollView addSubview:label_tip];
    
    UILabel *label_tip2 = [[[UILabel alloc]initWithFrame:CGRectMake(0, 105+40*lines, 140, 20)]autorelease];
    label_tip2.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
    label_tip2.text = @"是否需要违章票证 :";
    [_bgScrollView addSubview:label_tip2];
   
    
    UILabel *label_tip3 = [[UILabel alloc]initWithFrame:CGRectMake(180, 103+40*lines, 100, 30)];
    label_tip3.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
    label_tip3.text = @"不需要";
    label_tip3.textAlignment = NSTextAlignmentLeft;
    [_bgScrollView addSubview:label_tip3];
    
    
    UIButton* m_button = [UIButton buttonWithType:UIButtonTypeCustom];
    m_button.frame = CGRectMake(150,110+40*lines , 15, 15);
//    m_button.frame = CGRectMake(10,5, 15, 15);

    [m_button setBackgroundColor:[UIColor clearColor]];
    [m_button setBackgroundImage:[UIImage imageNamed:@"RadioButton-UnSelected"] forState:UIControlStateNormal ];
    [m_button setBackgroundImage:[UIImage imageNamed:@"RadioButton-selected"]forState:UIControlStateSelected];
    m_button.selected = NO;
    [m_button addTarget:self action:@selector(needPost:) forControlEvents:UIControlEventTouchUpInside];
    [_bgScrollView  addSubview:m_button];
}

-(void) initTableView:(NSArray*)namelist
{
    /*
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, [DeviceUtil getScreenWidth], 40*lines) style:UITableViewStylePlain];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [_bgScrollView addSubview:tableView];
     */
    if( [namelist count] > 0)
    {
        UIButton *bgBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        bgBtn.frame = CGRectMake(10, 46, 300, HEIGTH);
        [bgBtn setBackgroundImage:[UIImage imageNamed:@"filmd_02"] forState:UIControlStateNormal];
        [_bgScrollView addSubview:bgBtn];
        
        for( int j=0;j<2;j++)
        {
            UILabel *head = [[UILabel alloc]initWithFrame:CGRectMake(j*150, 0, 150, HEIGTH)];
            head.backgroundColor = [UIColor clearColor];
            head.textColor = [UIColor redColor];
            if( j == 0)
                head.text = @"违章时间";
            else
                head.text = @"金额";
            head.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
            head.textAlignment = NSTextAlignmentCenter;
            head.layer.borderWidth = 0.5f ;
            [bgBtn addSubview:head];
            [head release];
        }


    }
    
    for (int i  = 0; i < [namelist count]; i ++) {
        UIButton *bgBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        bgBtn.frame = CGRectMake(10, 46+HEIGTH+i*HEIGTH, 300, HEIGTH);
        [bgBtn setBackgroundImage:[UIImage imageNamed:@"filmd_02"] forState:UIControlStateNormal];
        bgBtn.tag = 300+i;
        [bgBtn addTarget:self action:@selector(detailMessage:) forControlEvents:UIControlEventTouchUpInside];
        [_bgScrollView addSubview:bgBtn];
        
        for( int j=0;j<2;j++)
        {
            UILabel *head = [[UILabel alloc]initWithFrame:CGRectMake(j*150, 0, 150, HEIGTH)];
            head.backgroundColor = [UIColor clearColor];
            head.textColor = [UIColor redColor];
            if( j == 0)
                //head.text = @"2012.07.30";
                head.text = [carViolation_model.violationInfoArray[i] objectAtIndex:9];
            else
            {
                head.text = [NSString stringWithFormat:@"%d元",[carViolation_model.postFee intValue]/100];
            }
            head.font = [UIFont fontWithName:@"Arial" size:15];   //设置内容字体和字体大小
            head.textAlignment = NSTextAlignmentCenter;
            head.layer.borderWidth = 0.5f ;
            [bgBtn addSubview:head];
            [head release];
        }
        
    }

}

-(void)detailMessage:(id)sender
{
    UIButton* btn = (UIButton*)sender;
    if(btn)
    {
        int index = btn.tag - 300;
        CarViolationInfoViewController *infoView = [[CarViolationInfoViewController alloc]initWithTableIndex:(index)];
        [self.navigationController pushViewController:infoView animated:YES];
    }

}

#pragma datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [carViolation_model.violationInfoArray count]+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *oneCell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    if(oneCell == nil)
    {
        oneCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];
        oneCell.frame = CGRectMake(0, 0, 300, 40) ;
        
        for(int i = 0 ; i < 2 ;i++)
        {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(oneCell.frame.size.width/2*i, 0, oneCell.frame.size.width/2, oneCell.frame.size.height)];
            label.textColor = [UIColor blackColor];
            label.backgroundColor = [UIColor clearColor];
            label.textAlignment = NSTextAlignmentCenter ;
            label.tag = 170 + i;
       //     label.layer.borderColor = [UIColor grayColor].CGColor;
       //     label.layer.borderWidth = 0.3 ;
            [oneCell addSubview:label];
        }
        
    }
    for(int i = 0 ; i < 2 ;i++)
    {
        UILabel *label = (UILabel *)[oneCell viewWithTag:170 + i] ;
        label.font = [UIFont systemFontOfSize:10];

        if( indexPath.row == 0 )
        {
            if( i == 0)
                label.text = @"违章时间";
            else
                label.text = @"金额";
        }
        else 
        {
            if( i == 0)
            {
 //              label.text = [carViolation_model.violationInfoArray[indexPath.row-1] objectAtIndex:9];
            }
            else
            {
                label.text = carViolation_model.postFee;
            }
        }

    }
    oneCell.layer.borderColor = [UIColor grayColor].CGColor;
    return oneCell ;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
    CarViolationInfoViewController *infoView = [[CarViolationInfoViewController alloc]initWithTableIndex:(indexPath.row-1)];
        [self.navigationController pushViewController:infoView animated:YES];
        //选中后的反显颜色即刻消失

    
}


-(void)confirmMessge:(id)sender
{
    CarViolationDetail2ViewController *detail = [[[CarViolationDetail2ViewController alloc]init]autorelease];
    [self.navigationController pushViewController:detail animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.0 ;
}
-(void)needPost:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
    if( button.selected)
    {
        carViolation_model.postMode = @"0";
        int total = 0;
        int principal = 0;
        int weiyue = 0;
        int service = 0;
        
        for( int i =0;i<[carViolation_model.violationInfoArray count];i++)
        {
            principal += [[carViolation_model.violationInfoArray[i] objectAtIndex:6] intValue];
            weiyue +=  [[carViolation_model.violationInfoArray[i] objectAtIndex:7] intValue];
            service +=  [[carViolation_model.violationInfoArray[i] objectAtIndex:8] intValue];
        }
        total =(principal+weiyue+service)/100;
        carViolation_model.totalAmount = [NSString stringWithFormat:@"%d",total];
    }
    else
    {
        carViolation_model.postMode = @"1";
        int total = 0;
        int principal = 0;
        int weiyue = 0;
        int service = 0;
        int postFee = [carViolation_model.postFee intValue];
        
        for( int i =0;i<[carViolation_model.violationInfoArray count];i++)
        {
            principal += [[carViolation_model.violationInfoArray[i] objectAtIndex:6] intValue];
            weiyue +=  [[carViolation_model.violationInfoArray[i] objectAtIndex:7] intValue];
            service +=  [[carViolation_model.violationInfoArray[i] objectAtIndex:8] intValue];
        }
        printf("%d,%d,%d,%d",principal,weiyue,service,postFee);
        total =(principal+weiyue+service+postFee)/100;
        carViolation_model.totalAmount = [NSString stringWithFormat:@"%d",total];
    }
}
@end
